package com.elinkway.infinitemovies.view;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.elinkway.infinitemovies.adaction.CommonAdActionHandle;
import com.elinkway.infinitemovies.bean.ServerPayload;
import com.elinkway.infinitemovies.selfdata.MyDataRecordCreater;
import com.elinkway.infinitemovies.selfdata.SelfDataConstant;
import com.elinkway.infinitemovies.selfdata.bean.MyDataRecord;
import com.elinkway.infinitemovies.selfdata.dao.RealmDaoManager;
import com.elinkway.infinitemovies.utils.UIs;
import com.elinkway.infinitemovies.utils.UmengEventPoster;
import com.elinkway.infinitemovies.utils.Utils;
import com.le123.ysdq.R;

import java.util.HashMap;

/**
 * Author: liuxiaojiang(xiao)
 * Date: 2017-04-12
 * Time: 19:12
 * Description: FIXME
 */
public class ServerNativeAdView {
    private Activity activity;
    private View mView;
    private long startRequestTime = 0;
    private String mAdposition;
    private String adclickUrl;

    public ServerNativeAdView(Activity activity, View view, String mAdposition){
        this.activity = activity;
        this.mView = view;
        this.mAdposition = mAdposition;
    }

    public ServerNativeAdView(Activity activity, String mAdposition){
        this.activity = activity;
        this.mAdposition = mAdposition;
    }

    public void setServerAd(final ServerPayload serverPayload, final String adPos){
        String adImg = serverPayload.pic;
        String adTitle = serverPayload.title;
        String adDesc = serverPayload.desc;
        final String adUrl = serverPayload.lp;
        if(mView == null){
            mView = activity.findViewById(R.id.nativeADContainerone);
        }
        adclickUrl = adUrl;
        AQuery aq = new AQuery(activity, mView);
        mView.setVisibility(View.VISIBLE);
        if("pause".equals(mAdposition)){
            mView.findViewById(R.id.pause_ad_image).setVisibility(View.VISIBLE);
            mView.findViewById(R.id.ad_close_iv).setVisibility(View.VISIBLE);
            mView.findViewById(R.id.ad_logo_iv).setVisibility(View.GONE);
            TextView tipsTv = (TextView) mView.findViewById(R.id.ad_tips_tv);
            RelativeLayout.LayoutParams tipsParams = (RelativeLayout.LayoutParams) tipsTv.getLayoutParams();
            tipsParams.addRule(RelativeLayout.ALIGN_LEFT, R.id.pause_ad_image);
            tipsTv.setLayoutParams(tipsParams);
            tipsTv.setVisibility(View.VISIBLE);
            mView.findViewById(R.id.baidu_inter_ad_rl).setVisibility(View.GONE);
            aq.id(R.id.pause_ad_image).image(adImg);
            mView.findViewById(R.id.ad_close_iv).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mView.setVisibility(View.GONE);
                    reportAdClose();
                }
            });
        }else if("focus".equals(mAdposition)){
            aq.id(R.id.ad_text_mark_tv_one).visible();
            aq.id(R.id.focus_item_image).image(adImg);
            aq.id(R.id.focus_item_tv).text(adTitle);
        }else if("homebanner".equals(mAdposition)){
            mView.setVisibility(View.VISIBLE);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, UIs.getScreenWidth()*5/18);
            mView.findViewById(R.id.home_banner_ad).setLayoutParams(params);
            aq.id(R.id.text_banner_desc).text(adDesc);
            aq.id(R.id.home_banner_ad).image(adImg);
            aq.id(R.id.ad_text_mark_tv_banner).visibility(View.GONE);
            aq.id(R.id.server_ad_tips).visibility(View.VISIBLE);
        }else{
            aq.id(R.id.text_desc_one).text(adDesc);
            aq.id(R.id.text_title_one).text(adTitle);
            aq.id(R.id.ad_left_image_iv_one).image(adImg);
            aq.id(R.id.ad_text_mark_tv_one).visibility(View.GONE);
            aq.id(R.id.server_ad_tips).visibility(View.VISIBLE);
        }
        reportAdShow();
        mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 点击响应
                if (!TextUtils.isEmpty(adUrl)){
                    CommonAdActionHandle.getInstance().doServerAd(activity, serverPayload, adPos);
                    reportAdClick();
                }
            }
        });
    }

    private void reportAdShow() {
        HashMap<String, String> gdtPropertyMap = new HashMap<>();
        gdtPropertyMap.put("position", mAdposition);
        UmengEventPoster.doPost(UmengEventPoster.SERVERAD_SHOW, gdtPropertyMap);

        MyDataRecord record = MyDataRecordCreater.build(MyDataRecord.class);
        record.setAcode(SelfDataConstant.AC_ADSHOW);
        record.setAd_pro(SelfDataConstant.AD_SERVER);
        record.setAd_url(adclickUrl);
        record.setAd_po(mAdposition);
        RealmDaoManager.addMyDataRecord(record);
    }

    private void reportAdClick() {
        HashMap<String, String> gdtPropertyMap = new HashMap<>();
        gdtPropertyMap.put("position", mAdposition);
        UmengEventPoster.doPost(UmengEventPoster.SERVERAD_CLICK, gdtPropertyMap);

        MyDataRecord record = MyDataRecordCreater.build(MyDataRecord.class);
        record.setAcode(SelfDataConstant.AC_ADCLICK);
        record.setAd_pro(SelfDataConstant.AD_SERVER);
        record.setAd_url(adclickUrl);
        record.setAd_po(mAdposition);
        RealmDaoManager.addMyDataRecord(record);
    }

    private void reportAdClose() {
        HashMap<String, String> gdtPropertyMap = new HashMap<>();
        gdtPropertyMap.put("position", mAdposition);
        UmengEventPoster.doPost(UmengEventPoster.SERVERAD_CLOSE, gdtPropertyMap);
    }
}
